<?php

require_once("config_inc.php");
require_once("core/forum_api.php");
require_once("core/user_api.php");
require_once("core/gui_api.php");

session_start();

if(isset($_POST["request"]) && $_POST["request"] == "add_user_comment")
{
	if(!is_active_user_session()) { exit(); }
	
	$db = new database();
	$db->connect();
		
	$forum_api = new forum_api($db);
	$comment_id = $forum_api->add_comment($_POST["project_id"], $_SESSION["user_id"], $_POST["comment_text"]);
		
	$comment_data = $forum_api->get_comment($comment_id);

	$db->close();
	
	$comment_html = NULL;
	
	if($comment_data)
	{	
		$gui_api = new gui_api(NULL);
	
		ob_start();
	
		$gui_api->insert_comment_node($comment_data["CMT_TEXT"], $comment_data["CMT_USR_ID"], $comment_data["USR_NAME"], NULL, $comment_data["CMT_DATE"]);
	
		$comment_html = ob_get_clean();
	}
					
	echo (json_encode(array("comment_html" => $comment_html)));
	exit();
}

if(isset($_POST["request"]) && $_POST["request"] == "get_comment_list")
{
	$db = new database();
	$db->connect();
	
	$gui_api = new gui_api($db);
	
	$comments_html = $gui_api->insert_forum_list($_POST["project_id"], $_POST["page"]);
	$paginator_html = $gui_api->insert_forum_pagination($_POST["project_id"], $_POST["page"]);
	
	$db->close();
	
	echo (json_encode(array("comments_html" => $comments_html, "paginator_html" => $paginator_html)));
	exit();
}

?>